package com.example.it.studymenu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class PopupMenuActivity extends AppCompatActivity {

    private RelativeLayout _rLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_menu);

        _rLayout = (RelativeLayout) findViewById(R.id.activity_popup_menu);

        _rLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //实例化PopupMenu
                PopupMenu pMenu = new PopupMenu(PopupMenuActivity.this, _rLayout);
                MenuInflater menuInflater = pMenu.getMenuInflater();
                menuInflater.inflate(R.menu.menu_mainactivity, pMenu.getMenu());

                //item的点击事件
                pMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        String title = (String) item.getTitle();
                        Toast.makeText(PopupMenuActivity.this, title, Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });
                //!!!!!!!!!!!!!注意一定要显示出来
                pMenu.show();

            }
        });
    }


}
